package com.laowang.project.system.controller;

import java.util.List;
import java.util.Objects;

import com.laowang.common.exception.CustomException;
import com.laowang.common.utils.SecurityUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.laowang.framework.aspectj.lang.annotation.Log;
import com.laowang.framework.aspectj.lang.enums.BusinessType;

import org.springframework.web.bind.annotation.*;
import com.laowang.project.system.domain.TbReceiveAddress;
import com.laowang.project.system.service.TbReceiveAddressService;
import com.laowang.framework.web.controller.BaseController;
import com.laowang.framework.web.domain.AjaxResult;
import com.laowang.common.utils.poi.ExcelUtil;
import com.laowang.framework.web.page.TableDataInfo;

/**
 * 收件地址Controller
 * 
 * @author laowang
 * @date 2022-12-21
 */
@Api(tags = "收件地址接口")
@RestController
@RequestMapping("/receiveAddress")
public class TbReceiveAddressController extends BaseController {

    @Autowired
    private TbReceiveAddressService tbReceiveAddressService;

    @ApiOperation(value = "小程序端 查询收件地址",notes = "")
    @GetMapping("/wxApp/query")
    public AjaxResult query() {
        Integer mid = SecurityUtils.getLoginUser().getWxUser().getMid();
        TbReceiveAddress tbReceiveAddress = new TbReceiveAddress();
        tbReceiveAddress.setUserId(mid);
        List<TbReceiveAddress> tbReceiveAddresses = tbReceiveAddressService.selectTbReceiveAddressList(tbReceiveAddress);
        return AjaxResult.success("操作成功",tbReceiveAddresses);
    }

    @ApiOperation(value = "小程序端 新增收件地址",notes = "")
    @PostMapping("/wxApp/add")
    public AjaxResult addAddress(@Validated @RequestBody TbReceiveAddress tbReceiveAddress) {
        Integer mid = SecurityUtils.getLoginUser().getWxUser().getMid();
        tbReceiveAddress.setUserId(mid);
        tbReceiveAddressService.insertTbReceiveAddress(tbReceiveAddress);
        return AjaxResult.success("操作成功");
    }

    @ApiOperation(value = "小程序端 修改收件地址",notes = "")
    @PostMapping("/wxApp/update")
    public AjaxResult update(@RequestBody TbReceiveAddress tbReceiveAddress) {
        if (Objects.isNull(tbReceiveAddress.getId())){
            throw new CustomException("要修改主键不能为空",400);
        }
        return toAjax(tbReceiveAddressService.updateTbReceiveAddress(tbReceiveAddress));
    }


    /**
     * 查询收件地址列表
     */
    @ApiOperation(value = "查询收件地址列表",notes = "")
    @PreAuthorize("@ss.hasPermi('system:receiveAddress:list')")
    @GetMapping("/list")
        public TableDataInfo list(TbReceiveAddress tbReceiveAddress) {
        startPage();
        List<TbReceiveAddress> list = tbReceiveAddressService.selectTbReceiveAddressList(tbReceiveAddress);
        return getDataTable(list);
    }
    
    /**
     * 导出收件地址列表
     */
    @ApiOperation(value = "导出收件地址列表",notes = "")
    @PreAuthorize("@ss.hasPermi('system:receiveAddress:export')")
    @Log(title = "收件地址", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(TbReceiveAddress tbReceiveAddress) {
        List<TbReceiveAddress> list = tbReceiveAddressService.selectTbReceiveAddressList(tbReceiveAddress);
        ExcelUtil<TbReceiveAddress> util = new ExcelUtil<TbReceiveAddress>(TbReceiveAddress.class);
        return util.exportExcel(list, "receiveAddress");
    }

    /**
     * 获取收件地址详细信息
     */
    @ApiOperation(value = "获取收件地址详细信息",notes = "")
    @PreAuthorize("@ss.hasPermi('system:receiveAddress:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(tbReceiveAddressService.selectTbReceiveAddressById(id));
    }

    /**
     * 新增收件地址
     */
    @ApiOperation(value = "新增收件地址",notes = "")
    @PreAuthorize("@ss.hasPermi('system:receiveAddress:add')")
    @Log(title = "收件地址", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TbReceiveAddress tbReceiveAddress) {
        return toAjax(tbReceiveAddressService.insertTbReceiveAddress(tbReceiveAddress));
    }

    /**
     * 修改收件地址
     */
    @ApiOperation(value = "修改收件地址",notes = "")
    @PreAuthorize("@ss.hasPermi('system:receiveAddress:edit')")
    @Log(title = "收件地址", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TbReceiveAddress tbReceiveAddress) {
        return toAjax(tbReceiveAddressService.updateTbReceiveAddress(tbReceiveAddress));
    }

    /**
     * 删除收件地址
     */
    @ApiOperation(value = "删除收件地址",notes = "")
    @PreAuthorize("@ss.hasPermi('system:receiveAddress:remove')")
    @Log(title = "收件地址", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(tbReceiveAddressService.deleteTbReceiveAddressByIds(ids));
    }
}
